package valid_contr
{
	import flash.events.*;
	import flash.geom.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.*;
	import mx.events.*;
	import mx.managers.*;
	import mx.validators.*;
	

	public class string_combobox extends ComboBox
	{
		
		[Bindable] public var _selectedValue:String;
    	private var bSelectedValueSet:Boolean = false;
    	private var bDataProviderSet:Boolean = false;
    	private var validatorobj:StringValidator = new StringValidator
		//private var errorTip:ToolTip
		private var errorTip:Text;
		private var tip_position:String
		
		public function string_combobox()
		{
			super();
			validatorobj.source = this;
			validatorobj.listener = undefined;
			validatorobj.property = "_selectedValue";
			//this.addEventListener(MouseEvent.CLICK, clearvalidation);
			this.addEventListener(FocusEvent.FOCUS_OUT, validate2);
			super.addEventListener(DropdownEvent.CLOSE, updateselectedvalue);
			super.addEventListener(mx.events.PropertyChangeEvent.PROPERTY_CHANGE, onpropertychange);
			
		}
		
		private function onpropertychange(e:PropertyChangeEvent){
			if(e.property=='selectedValue'){Alert.show('test');}
		}
		
		public function updateselectedvalue(e:Event):void{
			
			if(super.selectedItem is String){
         	_selectedValue = String(super.selectedItem);
         	}
         	else
         	{
         		if(super.selectedItem != null){
         		if (super.selectedItem.Id != null){
         		_selectedValue =  super.selectedItem.Id;
         		}
         		}
         	}
         	this.errorString = '';
			try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
         	validate();
			
		}
		
		override protected function commitProperties():void {
            // invoke ComboBox version 
            super.commitProperties();
 
            // If value set and have dataProvider 
            if (bSelectedValueSet && bDataProviderSet) {
               // Set flag to false so code won't be called until selectedValue is set again 
               bSelectedValueSet=false;
               // Loop through dataProvider 
               for (var i:int=0; i<this.dataProvider.length; i++) {
                  // Get this item's data 
                  var item:String = this.dataProvider[i];
 
                  // Check if is selectedValue 
                  if(item == _selectedValue) {
                     // Yes, set selectedIndex 
                     this.selectedIndex = i;
                     break;
                  }
               }
            }
         }
         
         
 
         // Trap dataProvider being set 
         override public function set dataProvider(o:Object):void {
            // invoke ComboBox version 
            super.dataProvider = o;
 
            // This may get called before dataProvider is set, so make sure not null and has entries 
            if (o!=null && o.length) {
               // Got it, set flag 
               bDataProviderSet = true;
			   if (bSelectedValueSet == true){
				   this.selectedValue = _selectedValue;
			   }
            }
         }
 
         // set for selectedValue 
         public function set selectedValue(s:String):void {
            // Set flag 
            bSelectedValueSet = true;
            // Save value 
            _selectedValue = s;
            
            if (super.dataProvider is ArrayCollection){
            var dp:ArrayCollection;
            dp = ArrayCollection(super.dataProvider);
            
            
            for (var j:int=0;j<dp.length;j++){	
            	if(dp[j].Id == s){selectedItem = dp[j];}
            }
            }
            // Invalidate to force commit 
            invalidateProperties();
            
            this.errorString = '';
			try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
         	validate();
			
         }

		
		private function validate2(e:Event):void{
			validate()
		}
		
		public function validate():Boolean
		{
			if (validatorobj.validate(null,true).type == ValidationResultEvent.INVALID){
			
			try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
				var pt:Point = new Point(0, 0);
        		pt = this.localToGlobal(pt);
				this.errorString = validatorobj.validate(null,true).message
				switch(tipposition){
					case "right":
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x + this.width + 5,pt.y,"errorTipRight") as ToolTip;
							errorTip = new Text();
							errorTip.text = validatorobj.validate(null,true).message;
							//errorTip.setStyle('fontSize',"14px");
							errorTip.setStyle('color','Red');
							this.parent.addChild(errorTip);
							errorTip.x = this.x + this.width + 5;
							errorTip.y = this.y
							break;
					case "top":
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x+5,pt.y - 45,"errorTipAbove") as ToolTip;
							break;
					case "bottom":
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x+5,pt.y + this.height + 5,"errorTipBelow") as ToolTip;
							break;
					default:
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x + this.width + 5,pt.y) as ToolTip;
							errorTip = new Text();
							errorTip.text = validatorobj.validate(null,true).message;
							//errorTip.setStyle('fontSize',"14px");
							errorTip.setStyle('color','Red');
							this.parent.addChild(errorTip);
							errorTip.x = this.x + this.width + 5;
							errorTip.y = this.y
				}
				errorTip.setStyle("styleName", "errorTip");

			return false
			}
			else
			{
				try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
			}
			return true
		}
		
		public function clearvalidation(e:Event):void{
			this.errorString = '';
			try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
			
			
		}
		
		public function get required():Boolean
		{
			return validatorobj.required;
		}

		public function set required(bool:Boolean):void
		{
			validatorobj.required = bool;
		}
		
		public function get minlength():String
		{
			return validatorobj.minLength.toString();
		}

		public function set minlength(length:String):void
		{
			validatorobj.minLength = length;
		}
		
		public function get maxlength():String
		{
			return validatorobj.maxLength.toString();
		}

		public function set maxlength(length:String):void
		{
			validatorobj.maxLength = length;
		}
		
		public function get tipposition():String
		{
			return tip_position;
		}

		public function set tipposition(tipposition2:String):void
		{
			tip_position = tipposition2;
		}
		
		
	}
}